//
//  FDModifyPasswordController.m
//  FamousDoctorOnline
//
//  Created by wszyxc on 16/7/11.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDModifyPasswordController.h"

#import <SVProgressHUD.h>

@interface FDModifyPasswordController ()

@property (weak,nonatomic) UITextField * oldField;

@property (weak,nonatomic) UITextField * currentField;

@property (weak,nonatomic) UITextField * sureField;

@property (weak,nonatomic) UIButton * sureBtn;


@end

@implementation FDModifyPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改密码";
    
    [self setupUI];
    
    [self setupLayout];
  
    
}


- (void)setupUI{
    
    //添加输入旧密码
    UITextField *oldField = [self setupOneTextFieldWithPlaceHolderText:@"请输入旧密码" backGroundImage:@"shurukuang"];
    
    self.oldField = oldField;
    
    [self.view addSubview:oldField];
  
    //添加输入新密码
    
    UITextField *currentField = [self setupOneTextFieldWithPlaceHolderText:@"请输入新登陆密码" backGroundImage:@"shurukuang"];
    
    self.currentField = currentField;
    
    [self.view addSubview:currentField];
    
    //添加输入确认密码
    UITextField *sureField = [self setupOneTextFieldWithPlaceHolderText:@"请再次输入新登陆密码" backGroundImage:@"shurukuang"];
    
    self.sureField = sureField;
    
    [self.view addSubview:sureField];
    
    //添加确认按钮
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.sureBtn = sureBtn;
    
    [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
    
    [sureBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    
    [sureBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    
    [self.view addSubview:sureBtn];
    
    [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    

}


- (void)sureClick{
    
//    //取出密码
//    NSArray *userInfo = [WZHUserDefaults WZHObjectForKey:@"userInfo"];
//    
//    //取出用户名和密码
//    NSString *getPassword = userInfo[kUserInfoPassWord];
    
    NSString *getPassword = [WZHUserDefaults WZHStringForKey:@"password"];
    
    if (self.oldField.text == getPassword) {
        //旧密码正确
        
        if(self.currentField.text == self.sureField.text){
            
            [WZHUserDefaults WZHSaveObject:self.currentField.text forKey:@"password"];
            
            [SVProgressHUD showErrorWithStatus:@"密码修改成功"];
            [self performSelector:@selector(dismissSV) withObject:nil afterDelay:1];
            
            [self.navigationController popViewControllerAnimated:YES];
            
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"两次输入不一致"];
            [self performSelector:@selector(dismissSV) withObject:nil afterDelay:1];
            
        }
        
    }else{
        
        [SVProgressHUD showErrorWithStatus:@"旧密码输入错误!"];
        
        [self performSelector:@selector(dismissSV) withObject:nil afterDelay:1];
        
        
    }
    
   
}

- (void)dismissSV{
    
    [SVProgressHUD dismiss];
    
}


- (UITextField *)setupOneTextFieldWithPlaceHolderText:(NSString *)holderText backGroundImage:(NSString *)imageName{
    
    UITextField *field = [[UITextField alloc]init];
    
    field.placeholder = holderText;
    
    field.background = [UIImage imageNamed:imageName];
    
//    UIImageView *leftImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"jinggao"]];
    
    field.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    
    field.leftViewMode = UITextFieldViewModeAlways;
    
    [self.view addSubview:field];
    
    return field;
}


- (void)setupLayout{
    
    
    [self.oldField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.offset(20);
        make.trailing.offset(-20);
        make.top.offset(84);
        make.height.offset(40);
        
    }];
    
    
    [self.currentField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.offset(20);
        make.trailing.offset(-20);
        make.top.equalTo(self.oldField.mas_bottom).offset(10);
        make.height.offset(40);
        
        
    }];
    
    [self.sureField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.offset(20);
        make.trailing.offset(-20);
        make.top.equalTo(self.currentField.mas_bottom).offset(10);
        make.height.offset(40);
        
        
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.offset(20);
        make.trailing.offset(-20);
        make.top.equalTo(self.sureField.mas_bottom).offset(50);
        make.height.offset(40);
        
    }];
    
    
}



@end
